// window.elements = {};//全局变量
$().ready(function () {
	var list = [
		{ "name": "commonManage", "text": "通用管理", "children": [] },
		{ "name": "regionManage", "text": "地区管理", "children": [] },
		{
			"name": "rpManage", "text": "权限管理", "children": [
				{ "name": "roleManage", "text": "角色管理" },
				{ "name": "userManage", "text": "用户管理" }]
		},
		{ "name": "tempManage", "text": "模板管理", "children": [] },
		{ "name": "machineManage", "text": "设备管理", "children": [] }
	];

	//退出
	$("#logout").click("on", function () {
		DIALOG.CONFIRM("退出确认提示：", "确定要退出当前登录吗？", function () {
			var userName = $("#logout").text();//密码text
			window.location.href = '/loginOut?userName=' + userName;
		});
	});

	//生产左边导航栏
	var menuListBox = $("#menu_list_box");
	_.map(list, function (item) {
		var itemName = item.name;
		var node = $("<div></div>");
		var element = $("<div class='list_item list_box'></div>");
		var spanBox = $("<span class='arrow'></span>");
		spanBox.append(item.text);
		element.append(spanBox);
		element.attr({
			style: " background-image: url(static/imgs/page_" + itemName + ".png);"
		});
		node.attr({
			id: itemName,
			'class': "mb-md-1 main_menu_box",
		});
		node.on('click', function () {
			changeMenu(item);
		});
		node.append(element);
		menuListBox.append(node);

		if (item.children && item.children.length > 0) {

			var subNode = $("<div></div>");
			subNode.attr({
				id: "sub_" + itemName
			});
			subNode.css({
				display: 'none'
			});

			_.map(item.children, function (child) {
				var childNode = $("<div></div>");
				var childElement = $("<div class='list_item sub_menu_box'></div>");
				var childSpan = $("<span class='arrow'></span>");
				childSpan.append(child.text);
				childElement.append(childSpan);
				childElement.attr({
					id: child.name,
					style: " background-image: url(static/imgs/page_" + child.name + ".png);"
				});
				childElement.on('click', function () {
					changeSubMenu(subNode, child.name, item);
				});
				childNode.append(childElement);
				subNode.append(childElement);
			});

			menuListBox.append(subNode);
		}
	});

	changeMenu(list[0]); // 默认第一个选中

	//节点选中触发
	function changeMenu(item) {

		var mainMenuBox = $(".main_menu_box");

		_.map(mainMenuBox, function (box) {

			var subMenu = $("#sub_" + box.id);
			if (box.id === item.name) {

				if (item.children && item.children.length > 0) {

					if (subMenu[0].style && subMenu[0].style.display === 'block') {
						changeMenu(list[0]);
					} else {
						if (subMenu[0].style) {
							subMenu[0].style.display = 'block';
						} else {
							subMenu[0].style = { display: 'block' };
						}
						changeSubMenu(subMenu, null, item);
					}
				} else {
					box.className += " menu_on_item";
					changeMenuBody(item);
				}
			} else {

				box.className = box.className.replace(" menu_on_item", "");

				if (subMenu[0]) {
					if (subMenu[0].style) {
						subMenu[0].style.display = 'none';
					} else {
						subMenu[0].style = { display: 'none' };
					}
				}
			}
		});
	}

	function changeSubMenu(subMenu, name, item) {

		var subNodes = subMenu[0].children;
		if (!name) { // 如果没有 name，则设置为第一个

			_.map(subNodes, function (subNode) {
				subNode.className = subNode.className.replace(" menu_on_item", "");
			});
			subNodes[0].className += " menu_on_item";
			changeMenuBody(item.children[0]);
		} else {
			for (var i = 0, len = subNodes.length; i < len; i++) {
				subNodes[i].className = subNodes[i].className.replace(" menu_on_item", "");
				if (subNodes[i].id === name) {
					subNodes[i].className += " menu_on_item";
					changeMenuBody(item.children[i]);
				}
			}
		}
	}

	function changeMenuBody(item) {

		var selectTreetitle = $("#selectTreetitle");
		selectTreetitle.html("");//清空
		selectTreetitle.html('<span id="selectedTitle" style="margin-left: 10%">' + item.text + '</span>');//赋值
	}
});

